<?php

/**
 * This is the model class for table "{{static_pages}}".
 *
 * The followings are the available columns in table '{{static_pages}}':
 * @property integer $staticpage_id
 * @property integer $category_id
 * @property string $title
 * @property string $content
 * @property string $slug
 * @property string $created_date
 * @property string $updated_date
 * @property string $read_count
 * @property integer $status
 */
class StaticPages extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return StaticPages the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{static_pages}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id, title, content,status,type', 'required'),
			array(' category_id, status ', 'numerical','integerOnly'=>true),
			array('title','length', 'max'=>100),
			array('title, content, created_date, updated_date','safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('staticpage_id, category_id, title, content, slug, created_date, updated_date,status, attachment', 'safe', 'on'=>'search'),
			array('updated_by, created_by', 'default', 'value' => ApplicationSessions::run()->read('id'), 'setOnEmpty' => false),
			array('updated_date', 'default', 'value' => new
            CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'update'), array('created_date, updated_date',
            'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' =>
            'insert'),			
			array('attachment', 'file', 'types' => 'jpeg,jpg,gif,png', 'allowEmpty' => true,
            'on' => 'insert,update'), 
            array('type', 'length', 'max' => 100),
			array('title,tags','filter','filter' => 'strip_tags'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		'category' => array(self::BELONGS_TO, 'Terms', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'staticpage_id' => 'Staticpage',
			'category_id' => 'Category id',
			'title' => 'Title',
			'content' => 'Content',
			'type' => 'Type',
			'attachment' => 'Attachment',
			'slug' => 'Slug',
			'created_date' => 'Created',
			'updated_date' => 'Modified',
			'tags'=>'Tags',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		$catID = null;
		//var_dump($this->category_id); exit;
        if (!empty($this->category_id))
        {

			$criteria1 = new CDbCriteria;
            $criteria1->select = 'termID';
            $criteria1->condition = 'term_name LIKE :name';
            $criteria1->params = array(':name' => '%' . $this->category_id . '%');
            $catID = Terms::model()->find($criteria1);
       
		}
		
		$criteria->compare('staticpage_id',$this->staticpage_id);
		//$criteria->compare('Terms.termID', $this->termID, true);
		//$criteria->compare('categoryID',$this->categoryID,true);
		
		if (!empty($catID))
        {
            $criteria->compare('category_id', $catID->termID);
        }
		else
		{
			$criteria->compare('category_id', $this->category_id);
		}

		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('slug',$this->slug,true);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('updated_date',$this->updated_date,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('updated_by',$this->updated_by,true);
		$criteria->compare('tags',$this->tags,true);
		$criteria->compare('status',$this->status,true);
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
        		'defaultOrder'=>'staticpage_id DESC',
        		 ),
		));
	}
	
	public function behaviors(){
    return array(
        'SlugBehavior' => array(
            'class' => 'application.models.SlugBehavior',
            'slug_col' => 'slug',
            'title_col' => 'title',
            'overwrite' => false
        )
    );
	}
	public function getModel($type, $arrParams = array())
    {
	$condition = "deactivate = 0 AND status = 1 ";
		switch ($type)
        {
            case 'STATIC-PAGE-ID':
                return StaticPages::model()->findByPk($arrParams['static_page_id']);
                break;
			case 'SLUG':
                return StaticPages::model()->find(array("condition"=>$condition." AND slug='".$arrParams['slug']."'"));
                break;
            case 'PARTICIPANT':
                return StaticPages::model()->find('participant_id = ? AND deactivate = 0 AND status = 1',array($arrParams['participant_id']));
                break;

        }
    }
	public static function getList($type, $arrParams = array())
    {
	$condition = "deactivate = 0 AND status = 1";
        switch ($type)
        {
            case 'ALL-PAGES':
                return StaticPages::model()->findAll(array("condition"=>$condition));
                break; 
				
			case 'ALL-CAT-PAGES':
                return StaticPages::model()->findAll(array("condition"=>"category_id='".$arrParams['termID']."'"));
                break;
				
			case 'SLUG':
                return StaticPages::model()->find(array("condition"=>$condition." AND slug='".$arrParams['slug']."'"));
                break;
        }
    }
    
	public static function getCount($type, $arrParams = array())
	{
		$date = $arrParams['todays_date'];
		switch ($type)
        {
            case 'COUNT':
				$q = new CDbCriteria;
				$q->addCondition('created_date LIKE :date');
				$q->addCondition('category_id = 1');
				$q->addCondition('deactivate = 0 AND status = 1');
				$q->params = array(':date' => "%$date%");
                return StaticPages::model()->findAll($q);
                break;
        }
	}
	
	public static function getJsonList($type)
    {
    	$condition = "deactivate = 0 AND status = 1";
    	switch ($type)
        {
        	case 'judge':
        		$jdata = JudgeMaster::model()->findAll(array('condition' => 'deactivate = 0 AND active = 1'));
        		$data = StaticPages::model()->findAll(array('condition' => $condition));
        		foreach($jdata as $k => $v)
        		{
        			foreach($data as $k1 => $v1)
        			{
        				$arr = explode(',,',$v1->judge_id);
        				if(in_array($v->judge_id, $arr))
        				{
        				 $dataarray[$v->slug][] = $v1->attributes;
        				}
        			}
        		
        		}
        		return $dataarray;
        		break;

        	case 'participant':
        		$jdata = ParticipantMaster::model()->findAll(array('condition' => 'deactivate = 0 AND active = 1'));
        		$data = StaticPages::model()->findAll(array('condition' => $condition));
        		foreach($jdata as $k => $v)
        		{
        			foreach($data as $k1 => $v1)
        			{
        				$arr = explode(',,',$v1->participant_id);
        				if(in_array($v->participant_id, $arr))
        				{
        				 $dataarray[$v->slug][] = $v1->attributes;
        				}
        			}
        		
        		}
        		return $dataarray;
        		break;
        		
        	case 'episode':
        		$jdata = EpisodeMaster::model()->findAll(array('condition' => 'deactivate = 0 AND active = 1'));
        		$data = StaticPages::model()->findAll(array('condition' => $condition));
        		foreach($jdata as $k => $v)
        		{
        			foreach($data as $k1 => $v1)
        			{
        				$arr = explode(',,',$v1->episode_id);
        				if(in_array($v->episode_id, $arr))
        				{
        				 $dataarray[$v->slug][] = $v1->attributes;
        				}
        			}
        		
        		}
        		return $dataarray;
        		break;	
        }
    }
}